import Vue from 'vue'
import Vuex from 'vuex'
import storage from '@/utils/Storage'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    cityname: '北京',
    user: storage.get('user'),  // 保存用户信息
    token: storage.get('TOKEN'), // 存储token字符串
    menu: storage.get('menu'), // 存储token字符串
  },
  getters: {
  },
  mutations: {

    /** 保存token */
    saveToken(state, payload) {
      state.token = payload
      storage.save('TOKEN', payload)
    },

    /** 保存用户信息  playload为用户对象 */
    saveUser(state, payload) {
      state.user = payload
      // 将user对象 转成json后 存入storage
      storage.save('user', payload)
    },

    // 用于保存cityname
    saveCity(state, payload) {
      state.cityname = payload
    },
    // 用于保存cityname
    saveMenu(state, payload) {
      state.menu = payload
      storage.save('menu', payload)

    },

  },
  actions: {
    /** 异步获取当前城市 */
    // getLocation(store) {
    //   setTimeout(() => {
    //     let city = '石家庄'
    //     store.commit('saveCity', city)
    //   }, 2000)
    // },

  },
  modules: {
  }
})
